<template>
  <TopBar>
    <template v-slot:back>
      <span class="back iconfont" @click="handleBackClick">&#xe6f2;</span>
    </template>
    <template v-slot:title>
      <p>管理收货地址</p>
    </template>
    <template v-slot:text>
      <router-link to="/editAddress">
        <span class="text">新建</span>
      </router-link>
    </template>
  </TopBar>
  <div class="wrapper">
    <div class="word">我的收货地址</div>
    <div class="container" v-for="item in addressList" :key="item._id">
      <router-link to="/editAddress">
        <span class="into iconfont">&#xe6f2;</span>
      </router-link>
      <div class="info">
        <div class="name">{{item.name}}</div>
        <span class="phone">{{item.phone}}</span>
      </div>
      <div class="address">
        <p>{{item.city}}{{item.department}}{{item.houseNumber}}</p>
      </div>
    </div>
  </div>
</template>

<script>
import TopBar from "../../components/TopBar";
import { useRouter } from "vue-router";
import { get } from "../../utils/request";
import { ref } from 'vue';
// 点击回退逻辑
const useBackRouterEffect = () => {
  const router = useRouter();
  const handleBackClick = () => {
    router.back();
  };
  return handleBackClick;
};
//获取用户地址列表
const getAddressListEffect = () => {
  const addressList = ref([]);
  const getList = async () => {
    const result = await get("/api/user/address");
    if (result?.errno === 0 && result?.data?.length) {
      addressList.value = result.data;
      console.log(addressList.value);
    }
  };

  return { addressList, getList}
};

export default {
  name: "MyAddress",
  components: { TopBar },
  setup() {
    const handleBackClick = useBackRouterEffect();
    const { addressList, getList} = getAddressListEffect();
    getList()
    return { handleBackClick, addressList };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
a {
  text-decoration: none;
}
.top {
  .back {
    font-size: 0.16rem;
    color: #b6b6b6;
  }
  p {
    flex: 1;
    font-size: 0.16rem;
    text-align: center;
    color: #333;
  }
  .text {
    font-size: 0.14rem;
    color: #333;
  }
}
.wrapper {
  overflow-y: auto;
  position: absolute;
  left: 0;
  top: 0.5rem;
  bottom: 0;
  right: 0;
  padding: 0.18rem;
  background-color: $content-bgColor;
  .word {
    margin-bottom: 0.18rem;
    font-size: 14px;
    color: #333333;
  }
  .container {
    margin-bottom: 0.16rem;
    position: relative;
    width: 100%;
    box-sizing: border-box;
    background: #fff;
    padding: 0.16rem;
    .into {
      position: absolute;
      height: 0.16rem;
      right: 0.16rem;
      top: 50%;
      transform: translate(0, -50%) rotate(180deg);
      font-size: 0.16rem;
    }
    .info {
      width: 100%;
      margin-bottom: 0.1rem;
      // height: 0.2rem;
      line-height: 0.2rem;
      .name {
        display: inline-block;
        width: .4rem;
        font-size: .14rem;
        color: #999999;
        margin-right: 0.74rem;
      }
      .phone {
        font-size: .14rem;
        color: #999999;
      }
    }
    .address {
      width: 2.6rem;
      line-height: 0.2rem;
      font-size: 14px;
      color: #333333;
      p {
        margin: 0;
      }
    }
  }
}
</style>